home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4107 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  88 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Two way communication between objects
  5. Date: 28 Jan 1996 01:51:48 GMT
  6. Organization: Kalevi, Inc
  7. Message-ID: <4eekrk$cu2@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe7.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 28, 1996 11:09:54 in article <Two way communication between
  15. objects>, 'Ross Forder <erosco@werple.mira.net.au>' wrote: 
  16.  
  17. >I am trying to write a VERY simple client and server set of objects. I  
  18. >would like to client to register with the server at ctor time and have  
  19. >the server know about the client by saving a pointer to the client  
  20. >so he can callback to a method called say 'event'.  
  21. >The problem is the fact that they will both need a pointer to each other  
  22. >and this seems impossible using strong typing. Is there a simple 'object  
  23. >oriented' way to do this? 
  24.  
  25. OO notwithstanding, a "pointer" to an object in another process, be it 
  26. on your home machine or across the net, is invalid -- at least as an 
  27. actual pointer.  Of course, you can pass a pointer to an object between 
  28. processes as a "handle", but the pointer is worthless to the other side. 
  29.  
  30. But your question is quite on a different subject; i.e., can two objects 
  31. have pointers to each other?  Sure can.  Example: 
  32.  
  33. class A;  // forward declaration 
  34.  
  35. class B { public: B(A* a) : a(a) {} private: A * a; }; 
  36.  
  37. class A { public: A() : b(0) {} void SetB(B* obj) {b = obj}; private: B* b;
  38. }; 
  39.  
  40. int main () 
  41.  { 
  42.    A a; 
  43.    B b(&a); 
  44.    a.SetB(&b); 
  45.    // Now, a has a pointer to b and b has a pointer to a 
  46.    return 0; 
  47.  } 
  48.  
  49. >I have been able to make this work by having the server only know about  
  50. >a parent class of the client (say eventhandler) but this is still not a  
  51. >bulletproof solution. 
  52. A bit of semantics here.  I believe that you mean knowing about the 
  53. parent instance rather than about the class.  Knowing about the parent's 
  54. class is meaningless as you can't call any of its members directly through 
  55. the pointer.   
  56.  
  57. The farther I get into trying to answer your question, the more 
  58. convinced I'm becoming that I don't really understand what you are 
  59. trying to do.  In a client-server environment, there is a connection 
  60. between the client and the server and information is passed 
  61. back and forth between the two in some predefined format which 
  62. both are able to handle.  Often this is is handled for the programmer 
  63. through a mechanism such as RPC.  RPC makes the interprocess 
  64. calls look just like local function calls.  For example, if you have 
  65. a function in the server whose prototype is 
  66. char * GetTheFoolsName(int id); 
  67. then you can just code char * name = GetTheFoolsName(5) and 
  68. RPC takes care of transporting a message to the server to 
  69. invoke its function and returning its result back to you.  But you 
  70. can't call member functions this way. 
  71.  
  72. >Can someone set me straight? 
  73. I don't think I did.  I really should trash this response, but what 
  74. the heck, it's Saturday and I'm tired.  I'll post it anyway. 
  75.  
  76. -- 
  77.  
  78. Pete 
  79.  
  80.  
  81.  
  82.  
  83.  
  84.